/****************************************************************
 * <strong>MFBase64管理类</strong>
 *
 * <p>
 * MFBase64管理类
 * </p>
 *
 * @author AndyZhang 2013
 * @version 0.1
 * ToDo:
 ***************************************************************/

package com.thinkwide.data.security;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class MFBase64 {
    public static String StringToBase64(String value) {
        if (value == null) return null;
        return new BASE64Encoder().encode(value.getBytes());
    }

    public static String Base64ToString(String value) throws IOException {
        if (value == null) return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b = decoder.decodeBuffer(value);
        return new String(b);
    }

    public static ByteArrayOutputStream FileToStream(String path) throws IOException {
        File f = new File(path);
        BufferedImage bi;
        bi = ImageIO.read(f);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, "jpg", baos);
        return baos;
    }

    public static boolean StreamToFile(ByteArrayInputStream bais, String path) throws IOException {
        BufferedImage bi1 = ImageIO.read(bais);
        File w2 = new File(path);
        ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动
        return true;
    }

    public static String ImageToBase64(ByteArrayOutputStream image) {
        byte[] bytes = image.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encodeBuffer(bytes).trim();
    }

    public static ByteArrayInputStream Base64ToImage(String value) {
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] b = decoder.decodeBuffer(value);

            ByteArrayInputStream bais = new ByteArrayInputStream(b);
            return bais;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String ImageToBase64(String path) throws IOException {
        ByteArrayOutputStream image = FileToStream(path);
        return ImageToBase64(image);
    }

    public static boolean Base64ToImage(String value, String path) throws IOException {
        ByteArrayInputStream image = Base64ToImage(value);
        return StreamToFile(image, path);
    }


    /**
     * <p>将文件转成base64 字符串</p>
     *
     * @param path 文件路径
     * @return
     * @throws Exception
     */
    public static String FileToBase64(String path) throws Exception {
        File file = new File(path);
        FileInputStream inputFile = new FileInputStream(file);
        byte[] buffer = new byte[(int) file.length()];
        inputFile.read(buffer);
        inputFile.close();
        return new BASE64Encoder().encode(buffer);
    }

    /**
     * <p>将base64字符解码保存文件</p>
     *
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */
    public static void Base64ToFile(String base64Code, String path) throws Exception {
        byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
        FileOutputStream out = new FileOutputStream(path);
        out.write(buffer);
        out.close();
    }

    /**
     * <p>将base64字符保存文本文件</p>
     *
     * @param base64Code
     * @param targetPath
     * @throws Exception
     */
    public static void toFile(String base64Code, String path) throws Exception {
        byte[] buffer = base64Code.getBytes();
        FileOutputStream out = new FileOutputStream(path);
        out.write(buffer);
        out.close();
    }

}
